

package com.hazelcast.map.impl.querycache.subscriber.record;

import com.hazelcast.internal.util.Clock;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

import static java.util.concurrent.atomic.AtomicIntegerFieldUpdater.newUpdater;


/**
 * Contains common functionality which is needed by a {@link QueryCacheRecord} instance.
 */
abstract class AbstractQueryCacheRecord implements QueryCacheRecord {

    private static final AtomicIntegerFieldUpdater<AbstractQueryCacheRecord> ACCESS_HIT = newUpdater(AbstractQueryCacheRecord.class, "hits");

    private final long creationTime;

    private volatile int hits;
    private volatile long accessTime = -1L;

    AbstractQueryCacheRecord() {
        creationTime = Clock.currentTimeMillis();
    }

    @Override
    public long getHits() {
        return hits;
    }

    @Override
    public long getLastAccessTime() {
        return accessTime;
    }

    @Override
    public long getCreationTime() {
        return creationTime;
    }

    @Override
    public void incrementAccessHit() {
        ACCESS_HIT.incrementAndGet(this);
    }

    @Override
    public void setAccessTime(long accessTime) {
        this.accessTime = accessTime;
    }
}
